﻿using Hammock;
using Hammock.Authentication.OAuth;
using Hammock.Web;



namespace MyMeetup
{

    public class MeetupOAuthRequest : MeetupRequest
    {
        private const string OAuthUrl = "https://api.meetup.com/oauth/";
        private const string CallbackUrl = "http://www.meetup.com";
        public MeetupOAuthRequest(string path)
        {
            // Use Hammock to set up our authentication credentials
            OAuthCredentials credentials = new OAuthCredentials()
                                               {
                                                   Type = OAuthType.RequestToken,
                                                   SignatureMethod = OAuthSignatureMethod.HmacSha1,
                                                   ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                                                   ConsumerKey = _key,
                                                   ConsumerSecret = _secret,
                                                   CallbackUrl = CallbackUrl
                                               };
            _client = new Hammock.RestClient()
            {
                Authority = OAuthUrl,
                Credentials = credentials
            };

            Path = path;

        }
    }

    public class MeetupRequest : RestRequest
    {
        internal RestClient _client;
        internal string _key = "qhjilk0g8ppffjq2f2eu52vc0s";
        internal string _secret = "p2677dcel6r3thhmshs13ebp37";

        internal const string ClientUrl = "https://api.meetup.com/";


        private OAuthCredentials GetCredentials()
        {
            var credentials = new OAuthCredentials
            {
                Type = OAuthType.ProtectedResource,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = _key,
                ConsumerSecret = _secret,
                Token = App.Settings.OAuthToken,
                TokenSecret = App.Settings.TokenSecret,
                Version = "1.0"
            };
            return credentials;
        }

        internal MeetupRequest(){}

        public MeetupRequest(string path, WebMethod method)
        {
            _client = new RestClient
                          {
                              Authority = ClientUrl,
                              HasElevatedPermissions = true,
                              Credentials = GetCredentials(),
                              Method = method
                          };
            this.Path = path;

        }

        public RestClient Client { get { return _client; } }
        

    }
}
